package com.my.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.my.dto.BasePageDTO;
import com.my.entity.UserEntity;
import com.my.service.UserService;
import com.my.vo.LayuiPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/student")
public class UserController {

    @Autowired
    private UserService studentService;

    @RequestMapping("/")
    public String toIndex(Model model) {
        UserEntity loginVO = (UserEntity) studentService.getById(1L);
        model.addAttribute("user", loginVO);
        return "student/index";
    }

    @RequestMapping("/toStudentList")
    public String toStudentList(Model model) {
        return "admin/studentList";
    }

    @RequestMapping("/toMyInfo")
    public String toMyInfo(Model model) {
        UserEntity stu = studentService.getById(1L);
        model.addAttribute("user", stu);
        return "student/myInfo";
    }


    @RequestMapping("/getStudentList")
    @ResponseBody
    public LayuiPageVO toStudentList(BasePageDTO dto, Model model) {
        LayuiPageVO vo = new LayuiPageVO();
        IPage<UserEntity> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<UserEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasLength(dto.getKeyword())) {
            queryWrapper.like(UserEntity::getName, dto.getKeyword())
                    .or().like(UserEntity::getSno, dto.getKeyword());
        }
        List<UserEntity> voList = studentService.getListByKeyword(dto.getKeyword());
        vo.setCode(0);
        vo.setCount(page.getTotal());
        vo.setMsg("ok");
        vo.setData(voList);
        return vo;
    }

}
